package cn.ekuma.epos.android.util;


import java.text.SimpleDateFormat;
import java.util.Date;

import cn.ekuma.android.ui.calendar.DateWidget;
import cn.ekuma.android.ui.calendar.DateWidget.OnSelectedDateListener;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

public class Utils {
	
    public final  static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:ss");
	
	public static int getScreenWidth(Context context) {
		WindowManager manager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		return display.getWidth();
	}
	
	public static int getScreenHeight(Context context) {
		WindowManager manager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		return display.getHeight();
	}
	
	public static float getScreenDensity(Context context) {
    	try {
    		DisplayMetrics dm = new DisplayMetrics();
	    	WindowManager manager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
	    	manager.getDefaultDisplay().getMetrics(dm);
	    	return dm.density;
    	} catch(Exception ex) {
    	
    	}
    	return 1.0f;
    }


	public static void popupDateWidget(Context context,
			View location, final OnSelectedDateListener onSelectedDateListener) {
		DateWidget dateWidget=new DateWidget(context);
		final PopupWindow mPopupWindow = new PopupWindow(dateWidget, LayoutParams.FILL_PARENT,  
                LayoutParams.WRAP_CONTENT);
		dateWidget.setOnSelectedDateListener(new OnSelectedDateListener(){
			
			public void OnSelectedDate(Date date) {
				mPopupWindow.dismiss();
				onSelectedDateListener.OnSelectedDate(date);
			}
		});
		 // ����������������ʧ  
		mPopupWindow.setOutsideTouchable(true);  
		mPopupWindow.setFocusable(true);  
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());  
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);  
        // ��ʾ��λ��Ϊ:��Ļ�Ŀ�ȵ�һ��-PopupWindow�ĸ߶ȵ�һ��  
        int xPos = windowManager.getDefaultDisplay().getWidth() / 2  
                - mPopupWindow.getWidth() / 2;  
        mPopupWindow.showAsDropDown(location, xPos, 0);  
        //mPopupWindow.showAtLocation(location, Gravity.CENTER, 0, 0); 
	}
}